
X=Xtrain;
t=Ytrain;
X2=Xtest;
t2=Ytest;

%% Free parameters
tau=0.016; %Gaussian parameter
C=16.4; %Regularization parameter


[n,d]=size(X);

%% Computation of the kernel
D=sum(X.*X,2);
Ones_n=ones(1,n);
A=0.5*D*Ones_n + 0.5*Ones_n'*D' - X*X';
K=exp(-tau*A);

%% Training
[alpha,b,errors,SVMcrits]=SMO(K,t,C);
support=find(alpha>1e-8 & alpha<C-1e-8);

%% Testing
y=K*(alpha.*t)-b;
good=find( (y>0 & t>0) | (y<0 & t<0) );
fprintf('Success rate (training set): %f \n',length(good)/n);

[n2,~]=size(X2);
D2=sum(X2.*X2,2);
Ones_n2=ones(1,n2);
A2=0.5*D2*Ones_n + 0.5*Ones_n2'*D' - X2*X';
K2=exp(-tau*A2); %Kernel coefficients for the testing set

y2=K2*(alpha.*t)-b;


good2=find( (y2>0 & t2>0) | (y2<0 & t2<0) );
fprintf('Success rate (testing set): %f \n',length(good2)/n2);

% J'ajoute un commentaire
